package com.chain.BrotherExchange.utils;

import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
import android.support.v7.widget.AppCompatImageView;
import android.view.View;
import android.widget.TextView;

import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.ui.core.BaseActivity;

public class ActivityBarHelper {

    public static void init(Activity a, @StringRes int stringRes) {
        a.findViewById(R.id.arrow_back_image).setOnClickListener(v -> a.onBackPressed());
        ((TextView) a.findViewById(R.id.title_text)).setText(stringRes);
    }

    public static void init(Activity a, @StringRes int stringRes, @StringRes int actionId,
                            View.OnClickListener onClickListener) {
        a.findViewById(R.id.arrow_back_image).setOnClickListener(v -> a.onBackPressed());
        ((TextView) a.findViewById(R.id.title_text)).setText(stringRes);
        TextView actionText = a.findViewById(R.id.action_text);
        actionText.setText(actionId);
        actionText.setOnClickListener(onClickListener);
    }

    public static void initial(Activity a, @StringRes int id, @DrawableRes int drawableRes,
                            View.OnClickListener onClickListener) {
        a.findViewById(R.id.arrow_back_image).setOnClickListener(v -> a.onBackPressed());
        ((TextView) a.findViewById(R.id.title_text)).setText(id);
        AppCompatImageView actionImage = a.findViewById(R.id.action_image);
        actionImage.setImageResource(drawableRes);
        actionImage.setOnClickListener(onClickListener);
    }

    /**
     * 透明背景、白色字体
     */
    public static void setTransparentStatus(Activity a) {
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = a.getWindow().getDecorView();
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            a.getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
    }

    /**
     * 透明背景、深色字体
     */
    public static void setTransparentDarkColorStatus(Activity a) {
        if (Build.VERSION.SDK_INT >= 23) {
            View decorView = a.getWindow().getDecorView();
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            a.getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
    }

    public static void setDarkColorStatus(Activity a) {
        if (Build.VERSION.SDK_INT >= 23) {
            View decorView = a.getWindow().getDecorView();
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
    }

    public static void setStatusColor(BaseActivity a, @ColorInt int color) {
        setStatusColor(a, color, false);
    }

    /**
     * 状态栏颜色和是否深色图标
     */
    public static void setStatusColor(BaseActivity a, @ColorInt int color, boolean isLightStatusBar) {
        if (Build.VERSION.SDK_INT >= 21) {
            a.getWindow().setStatusBarColor(color);

            if (Build.VERSION.SDK_INT >= 23 && isLightStatusBar) {
                View decorView = a.getWindow().getDecorView();
                decorView.setSystemUiVisibility(
                        View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                                | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            }
        }
    }

    /**
     * 白色背景、深色字体
     */
    public static void setWhiteDarkColorStatus(BaseActivity a) {
        if (Build.VERSION.SDK_INT >= 23) {
            View decorView = a.getWindow().getDecorView();
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            a.getWindow().setStatusBarColor(Color.WHITE);
        }
    }
}
